<script lang="ts" setup>
  import { createVNode } from 'vue';
  import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  import Button from '@sscd/button';
  import Space from '@sscd/space';
  import Modal from '@sscd/modal';
  const showConfirm = () => {
    Modal.confirm({
      title: 'Do you Want to delete these items?',
      icon: createVNode(ExclamationCircleOutlined),
      content: createVNode('div', { style: 'color:red;' }, 'Some descriptions'),
      onOk() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('OK');
        Modal.remove();
      },
      onCancel() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('Cancel');
        Modal.remove();
      },
      class: 'test',
    });
  };
  function showPromiseConfirm() {
    Modal.confirm({
      title: 'Do you want to delete these items?',
      icon: createVNode(ExclamationCircleOutlined),
      content: 'When clicked the OK button, this dialog will be closed after 1 second',
      async onOk() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        Modal.remove();
        try {
          return await new Promise((resolve, reject) => {
            setTimeout(Math.random() > 0.5 ? resolve : reject, 1000);
          });
        } catch {
          return console.log('Oops errors!');
        }
      },
      onCancel() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('Cancel');
        Modal.remove();
      },
    });
  }
  const showDeleteConfirm = () => {
    Modal.confirm({
      title: 'Are you sure delete this task?',
      icon: createVNode(ExclamationCircleOutlined),
      content: 'Some descriptions',
      okText: 'Yes',
      okType: 'danger',
      cancelText: 'No',
      onOk() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('OK');
        Modal.remove();
      },
      onCancel() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('Cancel');
        Modal.remove();
      },
    });
  };
  const showPropsConfirm = () => {
    Modal.confirm({
      title: 'Are you sure delete this task?',
      icon: createVNode(ExclamationCircleOutlined),
      content: 'Some descriptions',
      okText: 'Yes',
      okType: 'danger',
      okButtonProps: {
        disabled: true,
      },
      cancelText: 'No',
      onOk() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('OK');
        Modal.remove();
      },
      onCancel() {
        // vue3.4+版本可能会出现Modal无法正常关闭，需调用Modal.remove()
        console.log('Cancel');
        Modal.remove();
      },
    });
  };
</script>

<template>
  <div class="demo">
    <div class="demo-title">确认对话框</div>
    <div class="demo-content">
      <Space wrap>
        <Button @click="showConfirm">Confirm</Button>
        <Button @click="showPromiseConfirm">With promise</Button>
        <Button type="dashed" @click="showDeleteConfirm">Delete</Button>
        <Button type="dashed" @click="showPropsConfirm">With extra props</Button>
      </Space>
    </div>
  </div>
</template>
